package com.henz.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;
import com.henz.web.service.HenzReportService;
import com.henz.web.service.impl.HenzReportServiceImpl;
import com.henz.web.util.ChannelUtil;

@Controller
@RequestMapping("/report")
public class ReportController {
	
	Log logger = LogFactory.getLog(this.getClass());
	
	@RequestMapping("/{name}.do")
	public String viewPage(@PathVariable String name) {
		return "report/" + name;
	}
	
	@RequestMapping("/addSingle.json")  
	public @ResponseBody Map<String, Object> addSingle( 
		@RequestBody Map<String, Object> paramMap 
	) throws Exception{
		Map<String, Object> returnValue = null;
		List<Map<String, Object>> reportList = null;
		
		HenzReportService reportService = null;
		
		returnValue = new HashMap<String, Object>();
		
		reportService = new HenzReportServiceImpl();
		
		reportList = new ArrayList<Map<String, Object>>();
		
		reportList.add(paramMap);
		
		reportList = reportService.add(reportList);
		returnValue.put("result", "success");
		returnValue.put("data", reportList);
		
		return returnValue;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/add.json")  
	public @ResponseBody Map<String, Object> reportAdd( 
		@RequestBody Map<String, Object> paramMap 
	) throws Exception{
		Map<String, Object> returnValue = null;
		Object reportListObj = null;
		List<Map<String, Object>> reportList = null;
		
		HenzReportService reportService = null;
		
		reportService = new HenzReportServiceImpl();
		
		
		returnValue = new HashMap<String, Object>();
		reportListObj = paramMap.get("reportList");
		if(reportListObj instanceof List){
			reportList = (List<Map<String, Object>>)paramMap.get("reportList");
			reportList = reportService.add(reportList);
			returnValue.put("result", "success");
			returnValue.put("data", reportList);
		}else{
			returnValue.put("result", "classCastError");
			returnValue.put("data", reportListObj);
		}
		
		if(reportList != null && reportList.size() > 0){
			Map<String, Object> reportObj = reportList.get(0);
			String msg = "channel["
					+ reportObj.get("objKey")
					+ "] has created";
			ChannelService channelService = ChannelServiceFactory.getChannelService();
			//channelService.sendMessage(new ChannelMessage(channel, URLEncoder.encode(msg, "UTF-8")));
			//channelService.sendMessage(new ChannelMessage("henz.report", msg));
			
			JSONObject msgObj = null;
			
			msgObj = new JSONObject();
			
			msgObj.put("command", "created");
			msgObj.put("reportObj", reportObj);
			msgObj.put("message", msg);
			
			channelService.sendMessage(ChannelUtil.getChannelMessage("henz.report", msgObj));
		}
		
		
		return returnValue;
	}
	
	
	@RequestMapping("/list.json")  
	public @ResponseBody Map<String, Object> reportList( 
		@RequestBody Map<String, Object> paramMap 
	) throws Exception{
		logger.info("reportList paramMap[" + paramMap + "]");
		Map<String, Object> returnValue = null;
		returnValue = new HashMap<String, Object>();
		
		HenzReportService reportService = null;
		reportService = new HenzReportServiceImpl();
		
		List<Map<String, Object>> reportList = null;
		
		reportList = reportService.list();
		
		returnValue.put("reportList", reportList);
		
		return returnValue;
		
	}
	@RequestMapping("/listBak.json")  
	public @ResponseBody List<Map<String, Object>> reportListBak( 
		@RequestBody Map<String, Object> paramMap 
	) {
		logger.info("reportList paramMap[" + paramMap + "]");
		List<Map<String, Object>> jsonResult = null;
		Map<String, Object> data = null;
		jsonResult = new ArrayList<Map<String, Object>>();
		
		int rand;
		rand = 1 + (int)( 10 * Math.random() );
		
		double locLong = -96.7082683;
		double locLat = 32.980184099999995;
		
		try{
			locLong = Double.parseDouble( String.valueOf( paramMap.get( "CUR_LONG" ) ) );
		}catch(Exception e){}
		try{
			locLat = Double.parseDouble( String.valueOf( paramMap.get( "CUR_LAT" ) ) );
		}catch(Exception e){}
		
		
		for( int i = 0; i < rand; i ++ ){
			
			data = new HashMap<String, Object>();
			
			data.put("REPORT_TYPE", "TYPE[" + i + "]");
			data.put("REPORT_CITY", "CITY[" + i + "]");
			
			data.put("REPORT_LONG", ( locLong + ( Math.random() / 5 - 0.1 ) ) );
			data.put("REPORT_LAT" , ( locLat  + ( Math.random() / 5 - 0.1 ) ) );
			
			data.put("REPORT_NAME", "NAME[" + i + "]");
			data.put("REPORT_DESC", "DESC[" + i + "]");
			
			jsonResult.add(data);
			
		}
		
		return jsonResult;
	}
	
}